﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Xml.Linq;
using StartupSolution.ViewModel;
using System;

namespace StartupSolution.Views.Styling
{
    public partial class GridOffice2010BlueTheme : Page
    {
        public GridOffice2010BlueTheme()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            Loaded += new RoutedEventHandler(GridOffice2010BlueTheme_Loaded);
        }

        private void GridOffice2010BlueTheme_Loaded(object sender, RoutedEventArgs e)
        {
            /// <summary> 
            /// This method will load data from a XML document and populate it as XamGrid ItemSource
            /// </summary>
            LoadXMLData();

            // TODO: Apply the Office 2010 Blue theme to the xamGrid control.
            // This code is presented in the XAML page.
            // If you want to implement this functionality in the code-behind, comment the corresponding part in the XAML page.

            // LoadGridOffice2010Theme();
        }

        private void LoadGridOffice2010Theme()
        {
            ResourceDictionary resourceDictionary = new ResourceDictionary();
            resourceDictionary.Source = new Uri("/StartupSolution;component/Themes/Office2010Blue.xamGrid.xaml");

            LayoutRoot.Resources.MergedDictionaries.Add(resourceDictionary);
        }

        private void LoadXMLData()
        {
            // 	Loads the XML file and creates a XDocument object
            XDocument doc = XDocument.Load("Datasource/Products.xml");

            // Run LINQ query to retrieve the necessary data
            var data = (from d in doc.Descendants("Products")
                        select new Product
                        {
                            // Gets ProductID field
                            SKU = d.Element("ProductID").Value,
                            // Gets ProductName field
                            Name = d.Element("ProductName").Value,
                            // Gets Category field 
                            Category = d.Element("Category").Value,
                            // Gets Supplier field 
                            Supplier = d.Element("Supplier").Value,
                            // Gets QuantityPerUnit field
                            QuantityPerUnit = d.Element("QuantityPerUnit").Value
                            // TODO: Retrieve all the fields from XDocument and fill the XamGrid ItemSource
                        }).ToList<Product>();

            // Set the ItemsSource to generate the columns.
            igGrid.ItemsSource = data;
        }
    }
}
